//
//  MacroDefinitions.h
//  OCStarterProject
//
//  Created by huaya on 2025/10/2.
//

#ifndef MacroDefinitions_h
#define MacroDefinitions_h

#pragma mark - 系统版本宏定义

#define SS(strongSelf)__strong __typeof(weakSelf)strongSelf = weakSelf;


// App名称
#define App_Name WXLocalizedString(@"CFBundleDisplayName")



/// 系统版本
#define SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

/// iOS 版本判断
#define IOS_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define IOS_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define IOS_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define IOS_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

/// 具体版本判断
#define IOS_7_OR_LATER    (SYSTEM_VERSION >= 7.0)
#define IOS_8_OR_LATER    (SYSTEM_VERSION >= 8.0)
#define IOS_9_OR_LATER    (SYSTEM_VERSION >= 9.0)
#define IOS_10_OR_LATER   (SYSTEM_VERSION >= 10.0)
#define IOS_11_OR_LATER   (SYSTEM_VERSION >= 11.0)
#define IOS_12_OR_LATER   (SYSTEM_VERSION >= 12.0)
#define IOS_13_OR_LATER   (SYSTEM_VERSION >= 13.0)
#define IOS_14_OR_LATER   (SYSTEM_VERSION >= 14.0)
#define IOS_15_OR_LATER   (SYSTEM_VERSION >= 15.0)
#define IOS_16_OR_LATER   (SYSTEM_VERSION >= 16.0)
#define IOS_17_OR_LATER   (SYSTEM_VERSION >= 17.0)

#pragma mark - 设备类型宏定义

/// 设备类型
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)

/// 屏幕尺寸
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))

//  状态栏高度
#define kStatusBarHeight (([[UIApplication sharedApplication] isStatusBarHidden]) ? (IS_IPHONE_X ? 44.0 : 20.0) : [[UIApplication sharedApplication] statusBarFrame].size.height)

/// 设备型号判断
#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
#define IS_IPHONE_X ({\
BOOL temp = NO;\
if (@available(iOS 11.0, *)) {\
temp = ([kRCodeSync(@([[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom)) floatValue] > 0);\
}\
temp;\
})

#define IS_IPHONE_XR (IS_IPHONE && SCREEN_MAX_LENGTH == 896.0)
#define IS_IPHONE_12 (IS_IPHONE && SCREEN_MAX_LENGTH == 844.0)
#define IS_IPHONE_12_MAX (IS_IPHONE && SCREEN_MAX_LENGTH == 926.0)

//iPhone X适配 Nav 和 Tabber 高度和偏移数
#define PUB_NAVBAR_HEIGHT (IS_IPHONE_X?88.0f:64.0f)

#define PUB_NAVBAR_OFFSET (IS_IPHONE_X?24.0f:0.0f)

#define PUB_TABBAR_HEIGHT (50.0f + PUB_TABBAR_OFFSET)

#define PUB_TABBAR_OFFSET ({\
CGFloat temp = 0.0;\
if (@available(iOS 11.0, *)) {\
temp = [kRCodeSync(@([[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom)) floatValue];\
} else {\
temp = 0.0;\
}\
temp;\
})
// 在主线程中执行一段代码并返回对象(返回和接收值都是id类型)
#define kRCodeSync(x) ({\
    id __block temp;\
    if ([NSThread isMainThread]) {\
        temp = x;\
    } else {\
        dispatch_semaphore_t signal = dispatch_semaphore_create(0);\
        dispatch_async(dispatch_get_main_queue(), ^{\
            temp = x;\
            dispatch_semaphore_signal(signal);\
        });\
        dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);\
    }\
    temp;\
})
//主窗口
#define kMainWindow [TL_ViewHelper getKeyWindow]

//  AppDelegate
#define KAppDelegate [UIApplication sharedApplication].delegate

#pragma mark - 安全区域宏定义

// 固定单位
#define kMargin 20.0f

/// 安全区域
#define SAFE_AREA_TOP ({\
    CGFloat top = 0;\
    if (@available(iOS 11.0, *)) {\
        top = [UIApplication sharedApplication].keyWindow.safeAreaInsets.top;\
    }\
    top;\
})

#define SAFE_AREA_BOTTOM ({\
    CGFloat bottom = 0;\
    if (@available(iOS 11.0, *)) {\
        bottom = [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom;\
    }\
    bottom;\
})

#define SAFE_AREA_LEFT ({\
    CGFloat left = 0;\
    if (@available(iOS 11.0, *)) {\
        left = [UIApplication sharedApplication].keyWindow.safeAreaInsets.left;\
    }\
    left;\
})

#define SAFE_AREA_RIGHT ({\
    CGFloat right = 0;\
    if (@available(iOS 11.0, *)) {\
        right = [UIApplication sharedApplication].keyWindow.safeAreaInsets.right;\
    }\
    right;\
})

/// 状态栏高度
#define STATUS_BAR_HEIGHT ({\
    CGFloat height = 20;\
    if (@available(iOS 13.0, *)) {\
        height = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height;\
    } else {\
        height = [[UIApplication sharedApplication] statusBarFrame].size.height;\
    }\
    height;\
})

/// 导航栏高度
#define NAVIGATION_BAR_HEIGHT (44.0f)

/// 状态栏 + 导航栏高度
#define STATUS_NAV_BAR_HEIGHT (STATUS_BAR_HEIGHT + NAVIGATION_BAR_HEIGHT)

/// 标签栏高度
#define TAB_BAR_HEIGHT (49.0f + SAFE_AREA_BOTTOM)

#pragma mark - 颜色宏定义

/// RGB颜色
#define RGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

/// RGBA颜色
#define RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]

/// 十六进制颜色
#define HEXCOLOR(hex) [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16)) / 255.0 \
                                     green:((float)((hex & 0xFF00) >> 8)) / 255.0 \
                                      blue:((float)(hex & 0xFF)) / 255.0 alpha:1.0]

/// 十六进制颜色带透明度
#define HEXCOLORA(hex, a) [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16)) / 255.0 \
                                         green:((float)((hex & 0xFF00) >> 8)) / 255.0 \
                                          blue:((float)(hex & 0xFF)) / 255.0 alpha:(a)]

#define kBlackColor RGB(57, 56, 60)
#define kGrayTextColor RGB(176, 176, 176)
#define kGrayViewColor RGB(249, 249, 249)
#define kWhiteColor [UIColor whiteColor]
#define kMainColor RGB(158, 109, 252)
#define kGrayLineColor RGB(238, 238, 238)
#define kRedColor RGB(231, 85, 79)

#define kHalfMargin 10.0f
#define kMoreHalfMargin 15.0f
#define kQuarterMargin 5.0f

#define kCellLineHeight (IS_IPHONE_6?0.5f:0.4f)

#pragma mark - 字体宏定义

/// 系统字体
#define SYSTEM_FONT(size) [UIFont systemFontOfSize:size]

/// 粗体系统字体
#define BOLD_SYSTEM_FONT(size) [UIFont boldSystemFontOfSize:size]

/// 自定义字体
#define CUSTOM_FONT(name, size) [UIFont fontWithName:name size:size]

#define kMainFont [UIFont systemFontOfSize:14]
#pragma mark - 字符串定义
// 自动购买下一章
#define IS_AUTO_BUY_NEXT_CHAPTER @"is_auto_buy_next_chapter"

#pragma mark - 日志宏定义

/// 调试模式
#ifdef DEBUG
    #define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
    #define DLogFunc() NSLog(@"%s", __PRETTY_FUNCTION__)
#else
    #define DLog(...)
    #define DLogFunc()
#endif

/// 断言
#define ASSERT_MAIN_THREAD NSAssert([NSThread isMainThread], @"This method must be called on the main thread")

#pragma mark - 内存管理宏定义

/// 弱引用
#define WEAKSELF __weak typeof(self) weakSelf = self

/// 强引用
#define STRONGSELF __strong typeof(weakSelf) strongSelf = weakSelf

/// 释放对象
#define SAFE_RELEASE(obj) if (obj) { obj = nil; }

#define kNotification [NSNotificationCenter defaultCenter]

#pragma mark - 单例宏定义

/// 单例声明
#define SINGLETON_INTERFACE \
+ (instancetype)sharedManager;

/// 单例实现
#define SINGLETON_IMPLEMENTATION(className) \
static className *_instance_##className;\
+ (instancetype)allocWithZone:(struct _NSZone *)zone{\
static dispatch_once_t once_token_##className;\
dispatch_once(&once_token_##className, ^{\
_instance_##className = [super allocWithZone:zone];\
});\
return _instance_##className;\
}\
+ (instancetype)sharedManager{\
static dispatch_once_t once_token_##className;\
dispatch_once(&once_token_##className, ^{\
_instance_##className = [[self alloc] init];\
});\
return _instance_##className;\
}\
- (id)copyWithZone:(NSZone *)zone{\
return _instance_##className;\
}

#pragma mark - 自定义字符串

// apple ID
#define Apple_ID @"6456401162"
//  idfa
#define WX_IDFA_Code @"IDFA_Code"
#define WX_SITE_STATE @"wx_site_state"

// 好评页跳转地址
#define WX_EvaluationAddress [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id%@?mt=8", Apple_ID]


#pragma mark - 其他实用宏定义

/// 角度转弧度
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

/// 弧度转角度
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))

/// 字符串是否为空
#define STRING_IS_EMPTY(str) (str == nil || [str isKindOfClass:[NSNull class]] || ([str isKindOfClass:[NSString class]] && str.length == 0))

/// 数组是否为空
#define ARRAY_IS_EMPTY(arr) (arr == nil || [arr isKindOfClass:[NSNull class]] || ([arr isKindOfClass:[NSArray class]] && arr.count == 0))

/// 字典是否为空
#define DICTIONARY_IS_EMPTY(dict) (dict == nil || [dict isKindOfClass:[NSNull class]] || ([dict isKindOfClass:[NSDictionary class]] && dict.count == 0))

/// 获取应用版本号
#define APP_VERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

/// 获取应用构建版本号
#define APP_BUILD_VERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

/// 获取应用名称
#define APP_NAME [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]

/// 获取应用标识符
#define APP_BUNDLE_ID [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]

/// 主线程执行
#define DISPATCH_MAIN_THREAD(block) if ([NSThread isMainThread]) { block(); } else { dispatch_async(dispatch_get_main_queue(), block); }

/// 后台线程执行
#define DISPATCH_BACKGROUND_THREAD(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)

/// 延迟执行
#define DISPATCH_AFTER(delay, block) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), block)

#define kEmptyHoldImage [UIImage imageNamed:@"public_placehold_empty"]
// 判断对象是否为空
#define kObjectIsEmpty(object) !( \
([object respondsToSelector:@selector(length)] && [(NSData *)object length] > 0) || \
([object respondsToSelector:@selector(count)] && [(NSArray *)object count] > 0) || \
([object respondsToSelector:@selector(floatValue)] && [(id)object floatValue] != 0.0))

#define kiPhone6W 375.0
#define kiPhone6H 667.0
#define kScaleX (SCREEN_WIDTH / kiPhone6W)
#define kScaleY (SCREEN_HEIGHT / kiPhone6H)
//  比例高度
#define kGeometricHeight(viewWidth, width, height) (((viewWidth) * (height)) / (width))

//  比例宽度
#define kGeometricWidth(viewHeight, width, height) (((viewHeight) * (width)) / (height))

//  比例X坐标
#define kLineX(x) (x * kScaleX)

//  比例Y坐标
#define kLineY(y) (y * kScaleY)

//  将属性转换为字符串(PS:不要在属性的get方法里调用自身)
#define KEY_PATH(objc, property) ((void)objc.property, @(#property))
#pragma mark - 常量宏定义

#define kAnimatedDuration 0.4f

#define kAnimatedDurationFast 0.2f

// HUD停留时长
#define kHUDDuration 2.0

// HUD宽限时间
#define kHUDGraceTime 0.5

// HUD最少显示时长
#define kHUDMinShowTime 0.5

// XX秒后显示HUD的关闭按钮
#define kHUDMinCloseTime 1.0

#define kLabelHeight 30.0f
/// 默认头像
#define HoldUserAvatar [UIImage imageNamed:@"public_avatar"]
//  数据加解密
#define kEncryptionKey @"a9d1ddd1f27bbf66"
#define kEncryptionSecret @"3fb268a1557f7b9c"

#define WX_Facebook_Secret @"d33964a96efd4bf3cd622d4acff5be8a"
#define WX_Facebook_AppId @"9685353971527889"

#define WX_Google_ID @"615358868991-ljnbebde7vtjuq2gji247movhdhdaekq.apps.googleusercontent.com"
// App版本
#define App_Ver [NSString stringWithString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]]

#endif /* MacroDefinitions_h */
